import 'package:flutter/material.dart';
import '../utils/G.dart';

class OrderProvider with ChangeNotifier {
  Map _order = {}; // 订单信息
  Map _computedPrice = {}; // 计算后的价格
  Map _address = {}; // 收获地址
  Map _coupon = {}; // 优惠券
  String _payment = 'yue'; // 默认支付方式
  bool _useIntegral = false; // 是否使用积分抵扣
  String _remark = ""; // 订单备注

  Map get order => _order;
  Map get computedPrice => _computedPrice;
  Map get address => _address;
  Map get coupon => _coupon;
  String get payment => _payment;
  bool get useIntegral => _useIntegral;
  String get remark => _remark;

  // 初始化订单状态
  void initOrder(Map item) {
    _order = item;
    _address = item['addressInfo'];
  }

  // 计算订单价格
  void setOrderPrice() async {
    var res = await G.api.order.price(
        key: _order['orderKey'],
        addressId: _address['id'],
        payType: _payment,
        couponId: _coupon['id'] ?? 0,
        useIntegral: _useIntegral ? 1 : 0);
    if (res['status'] == 200) {
      _computedPrice = res['data']['result'];

      notifyListeners();
    }
  }

  // 设置收货地址 - 对应弹窗中的点选
  void setAddress(Map item) {
    _address = item;

    notifyListeners();
  }

  // 点选优惠券
  void setCoupon(Map item) {
    _coupon = item;

    notifyListeners();
  }

  // 订单备注
  void setRemark(String remark) {
    _remark = remark;
  }

  // 使用 | 取消 积分抵扣
  void setIntegral(bool flag) {
    _useIntegral = flag;

    notifyListeners();
  }

  // 点选支付方式
  void setPayment(String payname) {
    _payment = payname;

    notifyListeners();
  }
}
